package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
@Slf4j
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Resource
    StringRedisTemplate stringRedisTemplate;

    @Resource
    IUserService userService;

    private static final String FOLLOWS = "follows:";
    @Override
    public Result updateFollow(Long id, boolean isFollow) {
        Long userId = UserHolder.getUser().getId();
        // 关注
        if (isFollow) {
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(id);
            boolean save = save(follow);
            if (save) {
                // redis中保存关注的用户
                stringRedisTemplate.opsForSet().add(FOLLOWS + userId, id.toString());
            }
        } else {
            QueryWrapper<Follow> qw = new QueryWrapper<>();
            qw.eq("user_id", userId);
            qw.eq("follow_user_id", id);
            boolean remove = remove(qw);
            if (remove) {
                // redis中移除关注的用户
                stringRedisTemplate.opsForSet().remove(FOLLOWS + userId, id.toString());
            }
        }

        return Result.ok();
    }

    @Override
    public Result getFollow(Long id) {
        Long userId = UserHolder.getUser().getId();
        QueryWrapper<Follow> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        qw.eq("follow_user_id", id);
        // 有数据说明关注了
        Follow follow = getOne(qw);
        if (follow != null) {
            return Result.ok(true);
        }

        return Result.ok(false);
    }

    @Override
    public Result getCommonFollows(Long id) {
        Long userId = UserHolder.getUser().getId();

        Set<String> follows = stringRedisTemplate.opsForSet().intersect(FOLLOWS + userId, FOLLOWS + id);
        if (follows == null || follows.isEmpty()) {
            return Result.ok(Collections.emptyList());
        }

        List<Long> ids = follows.stream().map(Long::valueOf).collect(Collectors.toList());
        List<UserDTO> users = userService.listByIds(ids)
                .stream()
                .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                .collect(Collectors.toList());

        return Result.ok(users);
    }
}
